class Hotel < ActiveRecord::Base
  attr_accessible :x1, :x2, :y1, :y2, :w, :h, :user_id, :address, :area_id, :check_in_time, :check_out_time, :country_id, :description_eng, :description_idn, :district_id, :hotel_name, :hotel_rooms, :is_on_promoted, :is_published, :is_recomended, :postal_code, :primary_picture, :property_type_id, :province_id, :stars

  #validates :address, :area_id, :check_in_time, :check_out_time, :country_id, :description_eng, :description_idn, :district_id, :hotel_name, :hotel_rooms, :postal_code, :property_type_id, :province_id, :stars, :presence => true
  validates :stars, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 5 }

  attr_accessor :x1, :x2, :y1, :y2, :w, :h
  mount_uploader :primary_picture, ImageUploader

  after_update :crop_profile_picture

  belongs_to :user, foreign_key: "user_id"
  belongs_to :area, foreign_key: "area_id"
  belongs_to :property_type, foreign_key: "property_type_id"
  belongs_to :province, foreign_key: "province_id"
  belongs_to :district, foreign_key: "district_id"
  belongs_to :country, foreign_key: "country_id"

  has_many :hotel_images
  has_many :hotel_room_types, :dependent => :destroy
  has_many :hotel_facilities, :dependent => :destroy
  has_many :hotel_images, :dependent => :destroy

  def self.joining_and_search(search, pt, price_from, price_until, star_from, star_until, area)
    results = Hotel
    results = results.select("
            hotels.id as id,
            hotels.hotel_name as hotel_name,
            hotels.primary_picture as primary_picture,
            hotels.stars as stars,
            hotels.description_eng as description_eng,
            hotels.created_at as created_at,
            hotel_room_types.created_at as hrt_date_created,
            hotel_room_types.is_on_promoted as on_promoted,
            areas.area as area,
            districts.district_name as district_name,
            hotel_room_types.usd_promotion_price as promo_price,
            hotel_room_types.usd_price as usd_price,
            MIN( 
              CASE WHEN hotel_room_types.is_on_promoted = 't' THEN hotel_room_types.usd_promotion_price 
                ELSE hotel_room_types.usd_price END
            ) AS cheapest_price
          ").joins('INNER JOIN "hotel_room_types" ON "hotels"."id" = "hotel_room_types"."hotel_id" ')
            .joins('INNER JOIN "areas" ON "hotels"."area_id" = "areas"."id" ')
            .joins('INNER JOIN "districts" ON "hotels"."district_id" = "districts"."id" ')

    results = results.where("property_type_id IN (?)", pt) if !pt.blank?

    results = results.where("area_id IN (?)", area) if !area.blank?

    results = results.where("hotel_room_types.usd_promotion_price" => price_from..price_until) if !price_from.blank? and !price_until.blank?

    results = results.where("hotels.stars" => star_from..star_until) if !star_from.blank? and !star_until.blank?

    results = results.group("hotel_name")

    results
  end

  def crop_profile_picture
    if x1.present?
      primary_picture.recreate_versions! 
    end
  end
end
